<?php
/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://featherphp.googlecode.com/
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Manipulates HTTP headers (in, out).
 */
class HTTP
{
	protected static $codes;
	
	static function redirect($url='/', $status=null)
	{
		if (!is_null($status))
			self::status($status);
		
		if (DEBUG < 1)
			header("Location: $url");	
		else
		{
			Load::_load('views', 'view');
			$vars = array('url' => $url);
			$v = new View();
			$v->render('', 'redirect', $vars);
		}
		exit;
	}
	
	static function status($status)
	{
		if (empty(self::$codes))
			self::$codes = parse_ini_file(FEATHER.'config'.DS.'status.ini.php');
		header(self::$codes[$status]);
		return self::$codes[$status];
	}
	
	static function getStatus($status)
	{
		if (empty(self::$codes))
			self::$codes = parse_ini_file(FEATHER.'config'.DS.'status.ini.php');
		return self::$codes[$status];
	}
	
	static function getReferer()
	{
		return env('HTTP_REFERER');
	}
	
	static function isAjax()
	{
		return (env('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest');
	}
	
	static function getMethod()
	{
		return strtoupper(env('REQUEST_METHOD'));
	}
	static function isPost()
	{
		return (self::getMethod() === 'POST');
	}
	
	static function isGet()
	{
		return (self::getMethod() === 'GET');
	}
	
	// REST
	/*
	protected function data_to_postbody(&$data, $key='data')
	{
		$body = array();
		foreach($data as $k => $v)
		{
			if (is_array($v))
			{
				$body[] = self::data_to_postbody($v, $key.'['.urlencode($k).']');
				continue;
			}
			$body[] = $key.'['.urlencode($k).']='.urlencode($v);
		}
		return implode('&', $body);
	}
	*/
	/*
	protected function & postbody_to_data($body=null)
	{
		$_DATA = array();
		
		# get body
		if (is_null($body))
		{
			$body = '';
			$std_input = fopen('php://input', 'r');
			while ($tmp = fread($std_input, 1024))
				$body .= $tmp;
			fclose($std_input);
		}
		
		# parse it
		foreach(explode("&", $body) as $pair)
		{
			$pair = explode('=', $pair);
			
			# case: nested arrays
			preg_match_all('/\[(.*?)\]/', $pair[0], &$matches, PREG_SET_ORDER);
			if(!empty($matches))
			{
				# data
				preg_match('/^([^\[]+)\[/', $pair[0], &$var);
				$var = urldecode($var[1]);
				if (!isset($_DATA[$var]))
					$_DATA[$var] = array();
				$_tmp =& $_DATA[$var];
				
				# each [...]
				foreach($matches as $m)
				{
					if (!empty($m[1]))
					{
						$k = urldecode($m[1]);
						if (!isset($_tmp[$k]))
							$_tmp[$k] = '';
						$_tmp =& $_tmp[$k];
					}
					else {
						$_tmp =& $_tmp[];
					}
				}
				
				# set value (if any)
				$_tmp = isset($pair[1]) ? urldecode($pair[1]) : '';
				continue;
			}
			
			# case: just a variable (with or without value)
			if (isset($pair[1]))
				$_DATA[$pair[0]] = urldecode($pair[1]);
			else
				$_DATA[$pair[0]] = '';
		}
		return $_DATA;
	}
	*/
}
?>